#include "widget.h"
#include "ui_widget.h"
#include <QPushButton>
#include <QGridLayout>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

//    QPushButton *button1=new QPushButton("按钮1");
//    QPushButton *button2=new QPushButton("按钮2");
//    QPushButton *button3=new QPushButton("按钮3");
//    QPushButton *button4=new QPushButton("按钮4");

//    QGridLayout *glayout=new QGridLayout();
//    this->setLayout(glayout);//显示

//    //添加
////    glayout->addWidget(button1,0,0);
////    glayout->addWidget(button2,0,1);
////    glayout->addWidget(button3,1,0);
////    //即使这一个按钮，设置为100，100的位置，也不会在中间空出很大的空白空间
////    //此处设置的行数和列数，只是用来决定控件之间的相对位置
////    glayout->addWidget(button4,1,1);

////    //水平
////    glayout->addWidget(button1,0,0);
////    glayout->addWidget(button2,0,1);
////    glayout->addWidget(button3,0,2);
////    glayout->addWidget(button4,0,3);

////    //垂直
////    glayout->addWidget(button1,0,0);
////    glayout->addWidget(button2,1,0);
////    glayout->addWidget(button3,2,0);
////    glayout->addWidget(button4,3,0);

//    glayout->addWidget(button1,0,0);
//    glayout->addWidget(button2,1,1);
//    glayout->addWidget(button3,2,2);
//    glayout->addWidget(button4,3,3);

//    //创建6个按钮按照3*2排布
//    QPushButton *button1=new QPushButton("按钮1");
//    QPushButton *button2=new QPushButton("按钮2");
//    QPushButton *button3=new QPushButton("按钮3");
//    QPushButton *button4=new QPushButton("按钮4");
//    QPushButton *button5=new QPushButton("按钮5");
//    QPushButton *button6=new QPushButton("按钮6");

//    //创建网格布局的布局管理器
//    QGridLayout *glayout=new QGridLayout();
//    //显示
//    this->setLayout(glayout);
//    //添加
//    glayout->addWidget(button1,0,0);
//    glayout->addWidget(button2,0,1);
//    glayout->addWidget(button3,0,2);
//    glayout->addWidget(button4,1,0);
//    glayout->addWidget(button5,1,1);
//    glayout->addWidget(button6,1,2);

//    //设置列方向的拉伸系数
//    glayout->setColumnStretch(0,1);//设置第0行拉伸系数为1
//    glayout->setColumnStretch(1,2);//设置第1行拉伸系数为2
//    glayout->setColumnStretch(2,3);//设置第2行拉伸系数为3
//    //这里每行的拉伸系数比例就是 1：2：3

    //创建3*2个按钮
    QPushButton *button1=new QPushButton("按钮1");
    QPushButton *button2=new QPushButton("按钮2");
    QPushButton *button3=new QPushButton("按钮3");
    QPushButton *button4=new QPushButton("按钮4");
    QPushButton *button5=new QPushButton("按钮5");
    QPushButton *button6=new QPushButton("按钮6");

    //设置按钮水平垂直方向都尽可能多的拉伸
    button1->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    button2->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    button3->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    button4->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    button5->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    button6->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);

    //构建布局管理器
    QGridLayout *layout=new QGridLayout();
    //显示
    this->setLayout(layout);
    //添加
    layout->addWidget(button1,0,0);
    layout->addWidget(button2,0,1);
    layout->addWidget(button3,1,0);
    layout->addWidget(button4,1,1);
    layout->addWidget(button5,2,0);
    layout->addWidget(button6,2,1);

    //设置垂直拉伸系数
    layout->setRowStretch(0,1);
    layout->setRowStretch(1,2);
    layout->setRowStretch(2,3);

}

Widget::~Widget()
{
    delete ui;
}

